<?php

    require_once($_SERVER['DOCUMENT_ROOT'] . '/alpha/alert_sent.class.php');
    
    $action = $_GET['action'];
    switch ($action){
        case 'get':
            if(isset($_GET['val']) && $_GET['val'] == 'all'){
                // return list of all records (volonteurs)
                $mock = new AlertSentClass();
                echo json_encode($mock->getDBdata());
            }
            break;
        case 'sms':
            $ret_msg = '';
            if(isset($_GET['text']) && !empty($_GET['text'])){
                    $kyivstar_list = array();
                    $alpha_list = array();
                    $email_list = array();
                    if(isset($_GET['email']) && !empty($_GET['email'])){
                       $email_list = explode(';',$_GET['email']);
                    }
                    if(!empty($_GET['val'])){
                        $list = explode(';',$_GET['val']);
                        foreach($list as $k=>$v){
                            $code = substr($v,0,3);
                                if($code == '039' || 
                                    $code == '067' || 
                                    $code == '068' ||
                                    $code == '096' ||
                                    $code == '097' ||
                                    $code == '098' ){
                                        array_push($kyivstar_list, $v);
                                    }
                                    else{
                                        array_push($alpha_list, $v);
                                    }
                        }
                    }
                    $alert = new AlertSentClass();
                    if(count($kyivstar_list)>0){
                        $try = $alert->send_kyivstar($kyivstar_list, $alert->rus2translit($_GET['text']));
                        $ret_msg .= $try['msg'];
                    }
                    if(count($email_list)>0){
                        $try = $alert->send_email($email_list, $alert->rus2translit($_GET['text']), '');
                        $ret_msg .= $try['msg'];
                    }
                    if(count($alpha_list)>0){
                        foreach($alpha_list as $number){
                            $try = $alert->send_alfa($number, $alert->rus2translit($_GET['text']));
                            $ret_msg .= $try['msg'];
                        }
                    }
                }
                echo $ret_msg;
            break;
        default:
            echo 'Wrong action';
            break;
}
?>